/*
 ============================================================================
 Name		: DynamicListboxAppView.cpp
 Author	  : 
 Copyright   : Your copyright notice
 Description : Application view implementation
 ============================================================================
 */

// INCLUDE FILES
#include <coemain.h>
#include <aknlists.h> 
#include "DynamicListboxAppView.h"

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CDynamicListboxAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CDynamicListboxAppView* CDynamicListboxAppView::NewL(const TRect& aRect)
	{
	CDynamicListboxAppView* self = CDynamicListboxAppView::NewLC(aRect);
	CleanupStack::Pop(self);
	return self;
	}

// -----------------------------------------------------------------------------
// CDynamicListboxAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CDynamicListboxAppView* CDynamicListboxAppView::NewLC(const TRect& aRect)
	{
	CDynamicListboxAppView* self = new (ELeave) CDynamicListboxAppView;
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

// -----------------------------------------------------------------------------
// CDynamicListboxAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CDynamicListboxAppView::ConstructL(const TRect& aRect)
	{
	// Create a window for this application view
	CreateWindowL();

	iCount1 = 1;
	iCount2 = 100;
	
	iDesCArray = new(ELeave) CDesCArrayFlat( 5 );
	TBuf<128> buf;
	buf.Format( _L("\t%d.\t%d\t"),
		iCount1,
		iCount2 );
	
	iDesCArray->AppendL( buf );
	
	iListbox = new(ELeave) CAknDoubleStyleListBox;
	iListbox->ConstructL( this, EAknListBoxSelectionList );
	iListbox->SetContainerWindowL( *this );
	iListbox->CreateScrollBarFrameL( ETrue );
	iListbox->ScrollBarFrame()->SetScrollBarVisibilityL( CEikScrollBarFrame::EOn, 
		CEikScrollBarFrame::EAuto );

	
	CTextListBoxModel* model = iListbox->Model();
	model->SetItemTextArray( this );
	model->SetOwnershipType( ELbmDoesNotOwnItemArray );
	
	iListbox->HandleItemAdditionL();
	iListbox->ActivateL();
	
	// Set the windows size
	SetRect(aRect);
	
	// Activate the window, which makes it ready to be drawn
	ActivateL();
	}

// -----------------------------------------------------------------------------
// CDynamicListboxAppView::CDynamicListboxAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CDynamicListboxAppView::CDynamicListboxAppView()
	{
	// No implementation required
	}

// -----------------------------------------------------------------------------
// CDynamicListboxAppView::~CDynamicListboxAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CDynamicListboxAppView::~CDynamicListboxAppView()
	{
	if ( iDesCArray != NULL )
		{
		delete iDesCArray;
		iDesCArray = NULL;
		}
	
	if ( iListbox != NULL )
		{
		delete iListbox;
		iListbox = NULL;
		}
	}

TInt CDynamicListboxAppView::CountComponentControls() const
	{
	return 1;
	}

CCoeControl* CDynamicListboxAppView::ComponentControl(TInt aIndex) const
	{
	return iListbox;
	}

// -----------------------------------------------------------------------------
// CDynamicListboxAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CDynamicListboxAppView::Draw(const TRect& /*aRect*/) const
	{
//	// Get the standard graphics context
//	CWindowGc& gc = SystemGc();
//
//	// Gets the control's extent
//	TRect drawRect(Rect());
//
//	// Clears the screen
//	gc.Clear(drawRect);

	}

// -----------------------------------------------------------------------------
// CDynamicListboxAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CDynamicListboxAppView::SizeChanged()
	{
	//DrawNow();
	if( iListbox != NULL )
		{
		iListbox->SetExtent( TPoint( 0, 0 ),
			Rect().Size() );
		}
	}

// -----------------------------------------------------------------------------
// CDynamicListboxAppView::HandlePointerEventL()
// Called by framework to handle pointer touch events.
// Note: although this method is compatible with earlier SDKs, 
// it will not be called in SDKs without Touch support.
// -----------------------------------------------------------------------------
//
void CDynamicListboxAppView::HandlePointerEventL(
		const TPointerEvent& aPointerEvent)
	{

	// Call base class HandlePointerEventL()
	CCoeControl::HandlePointerEventL(aPointerEvent);
	}

TInt CDynamicListboxAppView::MdcaCount() const
	{
	return iDesCArray->Count();
	}

TPtrC16 CDynamicListboxAppView::MdcaPoint(TInt aIndex) const
	{
	return (*iDesCArray)[aIndex];
	}

void CDynamicListboxAppView::AddItem()
	{
	iCount1++;
	iCount2++;
	TBuf<128> buf;
	buf.Format( _L("\t%d.\t%d\t"),
		iCount1,
		iCount2 );
	
	iDesCArray->AppendL( buf );
	
	iListbox->HandleItemAdditionL();
	iListbox->DrawNow();
	}

void CDynamicListboxAppView::RemoveItem()
	{
	if ( iDesCArray->MdcaCount() > 0 )
		{
		iCount1--;
		iCount2--;
		iDesCArray->Delete( iDesCArray->MdcaCount() - 1 );
		iListbox->HandleItemRemovalL();
		iListbox->DrawNow();
		}
	}

TKeyResponse CDynamicListboxAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
	{
	return iListbox->OfferKeyEventL( aKeyEvent, aType );
	}

// End of File
